/*
 * Copyright (C) 2010, Funcom AS, All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  @authors
 *  Svein Gunnar Larsen <sveingunnarlarsen@gmail.com>
 *  Erik Thune Lund <culex.eriklund@gmail.com>
 *  Michel Krohn-Dale <kronicz@gmail.com>
 */

package com.funcom.peeler.action;

import com.funcom.peeler.WorkBench;
import com.funcom.peeler.peelable.PeelCanvasWindow;
import com.funcom.peeler.peelable.PeelComponent;
import com.funcom.peeler.peelable.PeelContainer;
import com.funcom.peeler.peelable.PeelWorkSpace;

import java.util.HashSet;
import java.util.Set;

public class Delete implements Change {

    PeelComponent component;
    PeelContainer old_parent;
    Set< PeelComponent > children;
    WorkBench.Bui bui;
    boolean deleteAll;

    public Delete( PeelComponent component, WorkBench.Bui bui, boolean deleteAll ) {

        if ( component == null ) throw new NullPointerException( "Delete must act on a valid component" );
        this.component = component;
        this.old_parent = component.getParent();
        this.bui = bui;
        this.deleteAll = deleteAll;

        if ( component instanceof PeelContainer ) {
            this.children = new HashSet< PeelComponent >( ((PeelContainer)component).getChildren() );
        }
    }

    public void doChange() {

        if ( component instanceof PeelWorkSpace ) return;
        if ( component instanceof PeelCanvasWindow) return;

        if ( bui != null ) bui.deselect( component );
        component.detach();

        if ( !deleteAll ) {
            if ( children != null ) {
                for ( PeelComponent child : children ) {
                    ((PeelContainer)component).removeChild( child );
                    old_parent.addChild( child );

                    child.getComponent().setLocation( child.getComponent().getX() + component.getComponent().getX(),
                                                        child.getComponent().getY() + component.getComponent().getY() );
                }
            }
        }
    }

    public void undoChange() {

        if ( component instanceof PeelWorkSpace ) return;
        if ( component instanceof PeelCanvasWindow) return;

        if ( old_parent != null ) {
            old_parent.addChild( component );
        }

        if ( !deleteAll ) {
            if ( children != null ) {
                for ( PeelComponent child : children ) {
                    child.getParent().removeChild( child );
                    ((PeelContainer)component).addChild( child );

                    child.getComponent().setLocation( child.getComponent().getX() - component.getComponent().getX(),
                                                        child.getComponent().getY() - component.getComponent().getY() );
                }
            }
        }
    }

    public void startChange() {
    }

    public void endChange() {
    }

    public boolean valid() {

        return true;
    }
}
